class Game {
    constructor() {
        this.gameDiv = document.getElementById("local_game");
        this.nextDiv = document.getElementById("local_next");
        this.timeDiv = document.getElementById("local_time");
        this.scoreDiv = document.getElementById("local_score");
        this.resultDiv = document.getElementById("local_gameover");
    }
    setTime(time) {
        this.timeDiv.innerHTML = time;
    }
    addScore(line) {
        let s = 0;
        switch (line) {
            case 1:
                s = 10;
                break;
            case 2:
                s = 30;
                break;
            case 3:
                s = 60;
                break;
            case 4:
                s = 100;
                break;
            default:
                break;
        }
        this.score += s;
        this.scoreDiv.innerHTML = this.score;
    }
    gameover(win){
        if(win){
            this.resultDiv.innerHTML = "你赢了";
        }else{
            this.resultDiv.innerHTML= "你输了";
        }
    }
    addTailLines(lines){
        for(let i =0;i<20-lines.length;i++){
            this.gameData[i] = this.gameData[i+ lines.length];
        }
        for(let i=0;i<lines.length;i++){
            this.gameData[20-lines.length+i] = lines[i];
        }
        this.cur.origin.x -= lines.length;
        if(this.cur.origin.x < 0){
            this.cur.origin.x = 0;
        }
        this.refreshDiv(this.gameData, this.gameDivs);
    }
    init() {
        let data1 = [];
        for (let i = 0; i < 20; i++) {
            data1[i] = [];
            for (let j = 0; j < 10; j++) {
                data1[i][j] = 0;
            }
        }
        this.gameData = data1;
        this.score = 0;
        this.gameDivs = [];
        this.nextDivs = [];
        this.next = getSquareInstance();//下一个square实例，包括num，origin，Data中的data
        this.cur = getSquareInstance();//当前方块实例
        this.setData();
        this.initDiv(this.gameDiv, this.gameData, this.gameDivs);
        this.initDiv(this.nextDiv, this.next.data, this.nextDivs);
        this.refreshDiv(this.gameData, this.gameDivs);
        this.refreshDiv(this.next.data, this.nextDivs);
    }
    //检测方框在pos为原点情况下，点x,y是否在 重叠框 里面
    check(pos, x, y) {
        if (pos.x + x < 0) {
            return false;
        } else if (pos.x + x >= 20) {
            return false;
        } else if (pos.y + y < 0) {
            return false;
        } else if (pos.y + y >= 10) {
            return false;
        } else if (this.gameData[pos.x + x][pos.y + y] === 1) {
            return false;
        } else {
            return true;
        }
    }
    //检测数据 不为0的点 是否在 重叠框 内    是否data中全部不为0的点是 都在重叠框内
    isValid(pos, data) {
        for (let i = 0; i < data.length; i++) {
            for (let j = 0; j < data[0].length; j++) {
                if (data[i][j] != 0) {
                    if (!this.check(pos, i, j)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    //若点在 重叠框 里面，清数据
    clearData() {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.check(this.cur.origin, i, j)) {
                    this.gameData[this.cur.origin.x + i][this.cur.origin.y + j] = 0;
                }
            }
        }
    }
    //若点在 重叠框 里面，设置数据
    setData() {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.check(this.cur.origin, i, j)) {
                    this.gameData[this.cur.origin.x + i][this.cur.origin.y + j] = this.cur.data[i][j];
                }
            }
        }
    }
    canDown() {
        let test = {};
        test.x = this.cur.origin.x + 1;
        test.y = this.cur.origin.y;
        return this.isValid(test, this.cur.data);
    }
    down() {
        //console.log(this.isValid)
        if (this.canDown()) {
            this.clearData();
            this.cur.origin.x++;
            //console.log(this.gameData)
            this.setData();
            this.refreshDiv(this.gameData, this.gameDivs);
            return true;
        } else {
            return false;
        }
    }
    fall() {
        while (this.down()) {
            this.down();
        }
    }
    canLeft() {
        let test = {};
        test.x = this.cur.origin.x;
        test.y = this.cur.origin.y - 1;
        return this.isValid(test, this.cur.data);
    }
    left() {
        //console.log(this.isValid)
        if (this.canLeft()) {
            this.clearData();
            this.cur.origin.y--;
            //console.log(this.gameData)
            this.setData();
            this.refreshDiv(this.gameData, this.gameDivs);
        }
    }
    canRight() {
        let test = {};
        test.x = this.cur.origin.x;
        test.y = this.cur.origin.y + 1;
        return this.isValid(test, this.cur.data);
    }
    right() {
        //console.log(this.isValid)
        if (this.canRight()) {
            this.clearData();
            this.cur.origin.y++;
            //console.log(this.gameData)
            this.setData();
            this.refreshDiv(this.gameData, this.gameDivs);
        }
    }
    canRotate() {
        let num = this.cur.num + 1;
        if (num == 4) {
            num = 0;
        }
        let test = this.cur.Data[num];
        return this.isValid(this.cur.origin, test);
    }
    rotate() {
        //console.log(this.isValid)
        if (this.canRotate()) {
            this.clearData();
            this.cur.num++;
            if (this.cur.num === 4) {
                this.cur.num = 0;
            }
            this.cur.data = this.cur.Data[this.cur.num];
            //console.log(this.gameData)
            this.setData();
            this.refreshDiv(this.gameData, this.gameDivs);
        }
    }

    fixed() {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.check(this.cur.origin, i, j)) {
                    if (this.gameData[this.cur.origin.x + i][this.cur.origin.y + j] == 2) {
                        this.gameData[this.cur.origin.x + i][this.cur.origin.y + j] = 1;
                    }
                }
            }
        }
        this.refreshDiv(this.gameData, this.gameDivs);
    }
    //消行
    checkClear() {
        let line = 0;
        for (let i = 19; i >= 0; i--) {
            let clear = true;
            for (let j = 0; j < 10; j++) {
                if (this.gameData[i][j] != 1) {
                    clear = false;
                    break;
                }
            }
            if (clear) {
                line++;
                for (let m = i; m > 0; m--) {
                    for (let n = 0; n < 10; n++) {
                        this.gameData[m][n] = this.gameData[m - 1][n];
                    }
                }
                i++;
            }
        }
        return line;
    }
    //结束
    checkGameOver() {
        let gameOver = false;
        for (let j = 0; j < 10; j++) {
            if (this.gameData[1][j] == 1) {
                gameOver = true;
            }
        }
        return gameOver;
    }
    //使用下一个方块
    performNext() {
        this.cur = this.next;
        this.setData();
        this.next = getSquareInstance();
        this.refreshDiv(this.gameData, this.gameDivs);
        this.refreshDiv(this.next.data, this.nextDivs);
    }
    initDiv(container, data, divs) {
        for (let i = 0; i < data.length; i++) {
            let div = [];
            for (let j = 0; j < data[0].length; j++) {
                let newNode = document.createElement("div");
                newNode.className = "none";
                newNode.style.top = (i * 20) + "px";
                newNode.style.left = (j * 20) + "px";
                container.appendChild(newNode);
                div.push(newNode);
            }
            divs.push(div);
        }
    }
    refreshDiv(data, divs) {
        for (let i = 0; i < data.length; i++) {
            for (let j = 0; j < data[0].length; j++) {
                if (data[i][j] === 0) {
                    divs[i][j].className = "none";
                } else if (data[i][j] === 1) {
                    divs[i][j].className = "done";
                } else if (data[i][j] === 2) {
                    divs[i][j].className = "current";
                }
            }
        }
    }
}